package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"hello/conf"
	"hello/lib/mysqlLib"
	"hello/lib/redisLib"
	"log"
	"strconv"
)

type NewbandController struct {
	beego.Controller
	PublicController
}

type Band struct {
	GatewayName string `json:"gateway_name"`
	GatewayId string `json:"gateway_id"`
	UserId string `json:"user_id"`
}


func (nc *NewbandController) Get() {
	nc.TplName = "newband.html"
	//nc.Ctx.WriteString("newband")
	cookie := nc.Ctx.GetCookie(conf.UseridIdCookieId)
	log.Printf("coookie is :%s", cookie)
	if cookie == "" {
		log.Printf("empty cookie")
	}
	userID,err := redisLib.Get(redisLib.DB0, cookie)
	if err != nil {
		log.Printf("get userid error: %s", err.Error())
	}
	if userID == ""{
		nc.Redirect("/home", 302)
	}
	nc.Data["userID"] = userID
}

func (nc *NewbandController) BandDevice() {
	//todo:request verify(use cookie)
	var bandReq Band
	data := nc.Ctx.Input.RequestBody
	log.Printf("post request data is :%s", string(data))
	err := json.Unmarshal(data, &bandReq)
	if err != nil {
		log.Printf("unmarshal bandreq data error :%s", err.Error())
		bandresponse := Response{}
		bandresponse.Status = "error"
		bandresponse.Errmsg = err.Error()
		nc.Data["json"] = &bandresponse
		nc.ServeJSON()
		//nc.response("error", err.Error())
		return
	}
	userid_int,_ := strconv.Atoi(bandReq.UserId)
	res, err := mysqlLib.Query("select id from hh_gateway_band_RT where gateway_id=? and user_id= ?;", bandReq.GatewayId, userid_int)
	if err != nil {
		log.Printf("query :%s",bandReq.UserId)
		log.Printf("query band_RT error: %s", err)
		bandresponse := Response{}
		bandresponse.Status = "error"
		bandresponse.Errmsg = err.Error()
		nc.Data["json"] = &bandresponse
		nc.ServeJSON()
		//nc.response("error", err.Error())
		return
	}
	if len(res) == 0 {
		err = mysqlLib.Exec("insert into hh_gateway_band_RT  (user_id, gateway_id, gateway_name) value (?, ?, ?);", userid_int, bandReq.GatewayId, bandReq.GatewayName)
		if err != nil {
			log.Printf("insert user band error: %s", err)
			bandresponse := Response{}
			bandresponse.Status = "error"
			bandresponse.Errmsg = err.Error()
			nc.Data["json"] = &bandresponse
			nc.ServeJSON()
			//nc.response("error", err.Error())
			return
		}
		bandresponse := Response{}
		bandresponse.Status = "ok"
		bandresponse.Errmsg = "band success"
		nc.Data["json"] = &bandresponse
		nc.ServeJSON()
		//nc.response("ok", "添加设备成功！")
		return
	}
	if len(res) >= 1 {
		err = mysqlLib.Exec("update hh_gateway_band_RT set gateway_name = ? where user_id = ? and gateway_id = ?;", bandReq.GatewayName, userid_int, bandReq.GatewayId)
		if err != nil {
			log.Printf("update band_RT error: %s", err)
			bandresponse := Response{}
			bandresponse.Status = "error"
			bandresponse.Errmsg = err.Error()
			nc.Data["json"] = &bandresponse
			nc.ServeJSON()
			//nc.response("error", err.Error())
			return
		}
		bandresponse := Response{}
		bandresponse.Status = "ok"
		bandresponse.Errmsg = "update success"
		nc.Data["json"] = &bandresponse
		nc.ServeJSON()
		//nc.response("ok", "更新绑定设备成功！")
		return
	}
	bandresponse := Response{}
	bandresponse.Status = "ok"
	bandresponse.Errmsg = "band success"
	nc.Data["json"] = &bandresponse
	nc.ServeJSON()
	//nc.response("ok", "绑定成功！")
	return
}



//func (nc *NewbandController) response(status string, errmsg string) {
//	bandresponse := BandResponse{}
//	bandresponse.Status = status
//	bandresponse.Errmsg = errmsg
//	nc.Data["json"] = &bandresponse
//	nc.ServeJSON()
//}

